package com.technoorbit.vbreakup;


import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class QuotesListItemActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	  
    	super.onCreate(savedInstanceState);       
    	setContentView(R.layout.crudondb);
    	SetListAdapter();
      
        
   }
   
    public void SetListAdapter()
    { 
    	try {
    		Bundle extras = getIntent().getExtras();
        	String Type = extras.getString("TYPE");
        	final String isCategory = extras.getString("Level");
        	//String isCategory = "0";
        	String URL="";
        	if(isCategory.equalsIgnoreCase("0"))
        	{
        		URL=String.format(URLOptions.str_QuoteTypeURL,Type);
        	}
        	else if(isCategory.equalsIgnoreCase("1"))
        	{
        		URL=String.format(URLOptions.str_QuotesURL,Type);
        	}
    		List<String> list=JSONfunctions.ParseJsonListData(URL, "Records");
        	final ListView lv1 = new ListView(QuotesListItemActivity.this);
        	setContentView(R.layout.quoteslistitems);
            lv1.setAdapter(new ArrayAdapter<String>(QuotesListItemActivity.this, R.layout.quoteslistitems,list));
          	setContentView(lv1);
          	
          	lv1.setOnItemClickListener(new OnItemClickListener()
          	{
          	public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
          	{
          		String text1=(String) lv1.getItemAtPosition(position);
             	String[] text=text1.split(":");
             if	(ListLevel.equals("1")){	
             	StartCategoryListActivity(text[0].replace(" ", ""));
             }
             else {
            	 StartTextDetailsActivity(text[1]);
            	
             }
             
          	}
          });
	}
		
    	
    	catch (Exception e) {
    		e.printStackTrace();
        
    	}
    }
    public static String ListLevel="1";
    public void StartCategoryListActivity(String strValue) {
    	Intent intent=new Intent(QuotesListItemActivity.this, QuotesListItemActivity.class);
		intent.putExtra("TYPE",strValue);
		intent.putExtra("Level","1");
		startActivity(intent);
		ListLevel="2";
	}
    
    public void StartTextDetailsActivity(String strValue) {
    	Intent intent=new Intent(QuotesListItemActivity.this, QuotesDetailActivity.class);
		intent.putExtra("Text",strValue);
		intent.putExtra("Level","2");
		startActivity(intent);
		
	}
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
        	if(ListLevel.equalsIgnoreCase("2"))
            	ListLevel="1";
        	Intent intent=new Intent(QuotesListItemActivity.this, QuotesMainActivity.class);
        	startActivity(intent);
        	
             //moveTaskToBack(true);
        	return true;
        }
        return super.onKeyDown(keyCode, event);
    }	  
}